package com.hadoop.temperature;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class RunJob {

	private static SimpleDateFormat dateFormat = new SimpleDateFormat();

	static class MapperJob extends Mapper<LongWritable, Text, KeyPair, Text> {
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			// 处理读取的一行数据
			String line = value.toString();
			// 每一行数据由制表符分割，所以需要分割
			String[] dataArray = line.split("\t");
			System.out.println("value = " + value.toString());
			System.out.println("data.length = " + dataArray.length);

			// 只处理符合条件的数据
			if (dataArray.length != 2) {
				return;
			}
			try {
				// 解析年份
				Date date = dateFormat.parse(dataArray[0]);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);

				// calendar.get{1}就是年份
				int year = calendar.get(1);
				System.out.println("year = " + year);

				// 解析温度
				String temperature = dataArray[1].substring(0, dataArray[1].indexOf("C"));
				System.out.println("temperature = " + temperature);

				// 创建符合对象键值KeyPair
				KeyPair kp = new KeyPair();
				kp.setYear(year);
				kp.setTemperature(Integer.parseInt(temperature));

				Text t1 = new Text(temperature);
				System.out.println("t1 = " + t1);

				context.write(kp, t1);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}// map class

	static class ReducerJob extends Reducer<KeyPair, Text, KeyPair, Text> {

		@Override
		protected void reduce(KeyPair key, Iterable<Text> values, Context context)
				throws IOException, InterruptedException {
			for (Text v : values) {
				context.write(key, v);
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Configuration conf = new Configuration();
		try {
			Job job = Job.getInstance(conf, "compute temperature");
			job.setJarByClass(RunJob.class);
			job.setMapperClass(MapperJob.class);
			job.setReducerClass(ReducerJob.class);

			job.setMapOutputKeyClass(KeyPair.class);
			job.setMapOutputValueClass(Text.class);

			job.setNumReduceTasks(3);
			job.setPartitionerClass(Partition.class);
			job.setSortComparatorClass(Sort.class);
			job.setGroupingComparatorClass(Group.class);

			// 输入输出文件路径
			FileInputFormat.addInputPath(job, new Path("/input/year-temp/"));
			FileOutputFormat.setOutputPath(job, new Path("/output/year-temp/"));

			System.exit(job.waitForCompletion(true) ? 0 : 1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
